/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.modules.admin.dto.ElevatorTlwBindingListDTO;
import org.springblade.modules.admin.entity.ElevatorTlwBinding;
import org.springblade.modules.admin.vo.ElevatorTlwBindingVO;
import org.springblade.modules.admin.mapper.ElevatorTlwBindingMapper;
import org.springblade.modules.admin.service.IElevatorTlwBindingService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯梯联网绑定信息 服务实现类
 *
 * @author ysq
 * @since 2024-11-13
 */
@Service
public class ElevatorTlwBindingServiceImpl extends BaseServiceImpl<ElevatorTlwBindingMapper, ElevatorTlwBinding> implements IElevatorTlwBindingService {

	@Override
	public IPage<ElevatorTlwBindingVO> selectElevatorTlwBindingPage(IPage<ElevatorTlwBindingVO> page, ElevatorTlwBindingVO elevatorTlwBinding) {
		return page.setRecords(baseMapper.selectElevatorTlwBindingPage(page, elevatorTlwBinding));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean bindingOrUnbinding(ElevatorTlwBindingListDTO elevatorTlwBinding) {
		Integer type = elevatorTlwBinding.getType();
		boolean binding = new Integer(1).equals(type);
		boolean unBinding = new Integer(2).equals(type);
		// 如果 type 不属于 1 或 2，则抛出异常
		if (!binding &&!unBinding) {
			throw new ServiceException("操作类型参数错误");
		}
		List<ElevatorTlwBinding> bindingTargetList = elevatorTlwBinding.getBinding();
		List<String> tlwElevatorIdList = bindingTargetList
			.stream()
			.map(ElevatorTlwBinding::getTlwElevatorId)
			.collect(Collectors.toList());
		List<Long> existElevatorIdList = bindingTargetList
			.stream()
			.map(ElevatorTlwBinding::getElevatorId)
			.collect(Collectors.toList());
		// 若 tlwElevatorIdList 与 existElevatorIdList 存在重复的元素，则抛出异常
		if (tlwElevatorIdList.size() != tlwElevatorIdList.stream().distinct().count() ||
			existElevatorIdList.size() != existElevatorIdList.stream().distinct().count()) {
			throw new ServiceException("存在重复的电梯梯联网绑定信息");
		}
		// 绑定的情况：先覆盖，再插入
		if (binding) {
			// 删除原有梯联网电梯id记录、电梯id记录
			this.remove(new LambdaQueryWrapper<ElevatorTlwBinding>()
				.in(ElevatorTlwBinding::getTlwElevatorId, tlwElevatorIdList)
			);
			this.remove(new LambdaQueryWrapper<ElevatorTlwBinding>()
				.in(ElevatorTlwBinding::getElevatorId, existElevatorIdList)
			);
            return this.saveBatch(bindingTargetList);
		}
		// 解绑的情况：删除
        return this.remove(new LambdaQueryWrapper<ElevatorTlwBinding>()
                .in(ElevatorTlwBinding::getTlwElevatorId, tlwElevatorIdList)
                .or()
                .in(ElevatorTlwBinding::getElevatorId, existElevatorIdList)
        );
    }

}
